package cn.aran.order.service.impl;

import cn.aran.order.domain.OrderPetAcquisition;
import cn.aran.order.domain.OrderProduct;
import cn.aran.order.domain.OrderProductAcquisition;
import cn.aran.order.mapper.OrderProductAcquisitionMapper;
import cn.aran.order.mapper.OrderProductAcquisitionMapper;
import cn.aran.order.mapper.OrderProductMapper;
import cn.aran.order.service.IOrderProductAcquisitionService;
import cn.aran.basic.service.impl.BaseServiceImpl;
import cn.aran.order.vo.OrderConfirmVo;
import cn.aran.pet.domain.Pet;
import cn.aran.pet.domain.SearchMasterMsg;
import cn.aran.pet.mapper.PetMapper;
import cn.aran.pet.mapper.SearchMasterMsgMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author aran
 * @since 2022-07-21
 */
@Service
public class OrderProductAcquisitionServiceImpl extends BaseServiceImpl<OrderProductAcquisition> implements IOrderProductAcquisitionService {

    @Autowired
    private OrderProductAcquisitionMapper orderProductAcquisitionMapper;

    @Autowired
    private OrderProductMapper orderProductMapper;

    @Override
    public void confirm(OrderConfirmVo orderConfirmVo) {
        // 1.修改订单状态、价格
        OrderProductAcquisition orderProductAcquisition = orderProductAcquisitionMapper.loadById(orderConfirmVo.getId());
        orderProductAcquisition.setState(1);
        orderProductAcquisition.setPrice(orderConfirmVo.getMoney());
        orderProductAcquisition.setPaytype(orderConfirmVo.getPayType());
        orderProductAcquisitionMapper.update(orderProductAcquisition);
        // 3.修改寻主消息状态为已完成
        OrderProduct orderProduct = orderProductMapper.loadById(orderProductAcquisition.getSearchMasterMsgId());
        orderProduct.setState(2);
        orderProductMapper.update(orderProduct);
    }

    @Override
    public void cancelOrder(Long id) {
    // 1.根据订单Id查询订单，修改订单信息
        OrderProductAcquisition orderProductAcquisition = orderProductAcquisitionMapper.loadById(id);
        orderProductAcquisition.setState(-1);
        orderProductAcquisitionMapper.update(orderProductAcquisition);
        // 2.将寻主消息的shopId置空，设置状态为已审核，归还到寻主池
        OrderProduct orderProduct = orderProductMapper.loadById(orderProductAcquisition.getSearchMasterMsgId());
        orderProduct.setShopId(null);
        orderProduct.setState(1);
        orderProductMapper.update(orderProduct);
    }
}
